package com.example.websocket.model;

import lombok.Data;

import java.io.Serializable;

/**
 * @author Liunh
 */
@Data
public class Message implements Serializable{

    private static final long serialVersionUID = -6451812593150428369L;

    /**
     * 信息来源
      */
    private String source;
    // 消息数据
    private MessageData data;
    // 发送目的地
    private String target;
    // 消息时间
    private String createtime;
    // 其他信息
    private String desc;

    //操作状态码
    private Integer code;

    public Message() {
    }

    public Message(String source, String target,
                   MessageData data) {
        super();
        this.source = source;
        this.target = target;
        this.data = data;
        this.desc = "";
        this.code = 200;
        this.createtime = String.valueOf(System.currentTimeMillis());
    }

    public Message(String source, String target,
                   MessageData data, String desc) {
        super();
        this.source = source;
        this.target = target;
        this.data = data;
        this.desc = desc;
        this.code = 200;
        this.createtime = String.valueOf(System.currentTimeMillis());
    }

    public Message(String source, String target,
                   MessageData data, String desc,
                   String createtime) {
        super();
        this.source = source;
        this.data = data;
        this.desc = desc;
        this.code = 200;
        this.target = target;
        this.createtime = createtime;
    }

    public static Message generateMessage(Message msg, WebResult result){
        msg.setData(
                new MessageData(
                        msg.getData().getType(),
                        msg.getData().getModel(),
                        result.getData(),
                        result.getCode()
                )
        );
        msg.setDesc(result.getMsg());
        return msg;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public MessageData getData() {
        return data;
    }

    public void setData(MessageData data) {
        this.data = data;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getCreatetime() {
        return createtime;
    }

    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "Message{" +
                "source='" + source + '\'' +
                ", data=" + data +
                ", target='" + target + '\'' +
                ", createtime='" + createtime + '\'' +
                ", desc='" + desc + '\'' +
                ", code=" + code +
                '}';
    }
}
